package com.ustcinfo.study.hive.r4.xuzongbao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class HiveConnection {

	//连接hive的url
	private static String url = "jdbc:hive2://192.168.6.100:10000/default";

	//连接hive的driver
	private static String driver = "org.apache.hive.jdbc.HiveDriver";

	//连接hive的用户名和密码，这里设为服务器的用户名和密码
	private static String username = "hadoop";
	private static String password = "hadoop123";

	public static void main(String[] args) {
		//查询同一域名下的id的数量
		String sql = "select domain_name,count(*) from dns_xuzongbao group by domain_name";
	//	String sql = "select id,count(*) from dns_xuzongbao group by id limit 5";
		Connection conn = null;
		try {
			Class.forName(driver);//注册驱动
			
			//获取连接
			conn = DriverManager.getConnection(url, username, password);
			Statement st = conn.createStatement();
			ResultSet rs = st.executeQuery(sql);
			while(rs.next()) {
				System.out.println(rs.getString(1)+":"+rs.getInt(2));
			}
			rs.close();
			st.close();
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

	}

}






